import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd/message';
import {FacilityMaintenanceRecordService} from '../../service/facility-maintenance-record.service';
import {FacilityMaintenanceRecordQueryForm} from '../../types/req/FacilityMaintenanceRecordQueryForm';
import {SharedModule} from '../shared.module';
import {FacilityMaintenanceRecord} from '../../types/resp/FacilityMaintenanceRecord';
import {Page} from '../../types/page';
import {NzModalService} from 'ng-zorro-antd/modal';
import {MODAL_WIDTH} from '../../const';
import {FacilitySelectorComponent} from '../facility/facility-selector.component';
import {StaffSelectorComponent} from '../staff/staff-selector.component';

@Component({
  selector: 'facility-maintenance-record-selector',
  imports: [
    FacilitySelectorComponent,
    StaffSelectorComponent,
    SharedModule
  ],
  templateUrl: './facility-maintenance-record-selector.component.html',
  standalone: true
})
export class FacilityMaintenanceRecordSelectorComponent implements OnInit {
  searchForm!: FormGroup;
  searchLoading: boolean = false
  listOfData: FacilityMaintenanceRecord[] = [];
  total: number = 0
  isVisible: boolean = false
  title: string = '公共设施保养记录选择器(双击行选中)'
  modalWidth: string = MODAL_WIDTH

  @ViewChild(FacilitySelectorComponent, { static: false }) facilitySelectorComponent!: FacilitySelectorComponent;
  @ViewChild(StaffSelectorComponent, { static: false }) staffSelectorComponent!: StaffSelectorComponent;

  @Output() facilityMaintenanceRecordSelectedEvent = new EventEmitter<{}>();

  constructor(
    private fb: FormBuilder,
    private message: NzMessageService,
    private facilityMaintenanceRecordService: FacilityMaintenanceRecordService,
    private modal: NzModalService
  ) {}

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      facilityId: [0],
      facilityName: [''],
      staffId: [0],
      staffName: [''],
    })
    this.onSearch()
  }

  onReset() {
    this.searchForm.reset()
  }

  onSearch() {
    try {
      this.searchLoading = true
      this.facilityMaintenanceRecordService.find(new FacilityMaintenanceRecordQueryForm(this.searchForm.value)).subscribe({
        next: (resp) => {
          if (!resp || resp.code !== 1) {
            this.message.error(resp && resp.msg ? resp.msg : '操作异常')
            return
          }
          const page: Page<FacilityMaintenanceRecord> | undefined = resp.data
          if (page && page.list.length > 0) {
            this.listOfData = page.list
            this.total = page.total
          }
        },
        complete: () => {
          this.searchLoading = false
        }
      })
    } catch (e) {
      console.log('获取数据异常', e)
    }
  }

  onRowDblClick(row: FacilityMaintenanceRecord, event: MouseEvent) {
    this.isVisible = false
    this.facilityMaintenanceRecordSelectedEvent.emit({
    });
  }

  onBack() {
    this.isVisible = false
  }

  display() {
    this.isVisible = true
    this.searchForm.reset();
  }

  findFacility() {
    this.facilitySelectorComponent.display()
  }

  handleFacilitySelectedEvent(selectedFacility: { facilityId?: number; facilityName?: string; }) {
    if (selectedFacility && 'facilityId' in selectedFacility) {
      this.searchForm.patchValue(selectedFacility);
    }
  }
  findStaff() {
    this.staffSelectorComponent.display()
  }

  handleStaffSelectedEvent(selectedStaff: { staffId?: number; staffName?: string; }) {
    if (selectedStaff && 'staffId' in selectedStaff) {
      this.searchForm.patchValue(selectedStaff);
    }
  }
}
